package com.unipay.hosp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unipay.common.handler.UnipayException;
import com.unipay.common.result.ResultCodeEnum;
import com.unipay.hosp.repository.HospitalRepository;
import com.unipay.hosp.mapper.HospitalSetMapper;
import com.unipay.hosp.service.HospitalSetService;
import com.unipay.model.hosp.Hospital;
import com.unipay.model.hosp.HospitalSet;
import com.unipay.vo.order.SignInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Project: yitong_parent
 * @Author: C.yu
 * @Time: 2023-03-18 18:48:50
 * @Desc: TODO
 */
@Transactional
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService{

     // TODO 原始方式：注入   ---这里，service调mapper已经做了封装---baseMapper
    @Autowired
    private HospitalSetMapper hospitalSetMapper;
    @Autowired
    private HospitalRepository hospitalRepository;

    // 更新状态：ID查询、更改状态、调用方法修改
    @Override
    public int updateStatus(Long id, Integer status) {
        HospitalSet hospitalSet = baseMapper.selectById(id);
        hospitalSet.setStatus(status);
        int i = baseMapper.updateById(hospitalSet);
        return i;
    }

    @Override
    public String getSignKey(String hoscode) {
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hoscode", hoscode);
        HospitalSet hospitalSet = baseMapper.selectOne(queryWrapper);
        //---判断
        if (null == hospitalSet){
            throw new UnipayException(ResultCodeEnum.HOSPITAL_OPEN);    //未开通，暂时不能访问
        }
        if (hospitalSet.getStatus().intValue() == 0){
            throw new UnipayException(ResultCodeEnum.HOSPITAL_LOCK);    //status=0，禁用，不能访问
        }
        return hospitalSet.getSignKey();
    }

    @Override
    public Hospital getHospitalByHoscode(String hoscode) {
        System.out.println("hoscode: " + hoscode);
        Hospital hospitalByHoscode = hospitalRepository.getHospitalByHoscode(hoscode);
        System.out.println("----: " + hospitalByHoscode);
        return hospitalByHoscode;
    }


    // 获取：医院签名信息
    @Override
    public SignInfoVo getSignInfoVo(String hoscode) {
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        wrapper.eq("hoscode", hoscode);
        HospitalSet hospitalSet = baseMapper.selectOne(wrapper);
        if (null == hoscode){
            throw new UnipayException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        SignInfoVo signInfoVo = new SignInfoVo();
        signInfoVo.setApiUrl(hospitalSet.getApiUrl());
        signInfoVo.setSignKey(hospitalSet.getSignKey());
        return signInfoVo;
    }
}
